package com.ruoyi.system.utils;

import com.ruoyi.system.domain.Subjects;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Util {


    /**
     * 解压缩文件到指定目录
     *
     * @param zipFilePath   压缩文件路径
     * @param destDirectory 目标目录路径
     * @throws IOException
     */
    public static void unzip(String zipFilePath, String destDirectory) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
        ZipEntry entry = zipIn.getNextEntry();

        while (entry != null) {
            String filePath = destDirectory + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // 如果是文件，创建父目录
                new File(filePath).getParentFile().mkdirs();
                extractFile(zipIn, filePath);
            } else {
                // 如果是目录，创建目录
                File dir = new File(filePath);
                dir.mkdirs();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();
    }

    /**
     * 解压缩文件
     *
     * @param zipIn    ZipInputStream对象
     * @param filePath 解压缩后文件路径
     * @throws IOException
     */
    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[4096];
        int read;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }


    // 生成指定长度的随机字符串
    public static String generateRandomString(int length) {
        // 定义随机字符串的字符集
        String charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        // 创建Random对象
        Random random = new Random();

        // 使用StringBuilder来构建字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            // 从字符集中随机获取字符并添加到StringBuilder
            int randomIndex = random.nextInt(charset.length());
            char randomChar = charset.charAt(randomIndex);
            sb.append(randomChar);
        }
        return sb.toString();
    }


    public static Map<String,Object> returnmap(Long status,Long queue){
        Map<String, Object> result = new HashMap<>();
        boolean isrun = false;
        if(status==4)
            isrun = true;
        result.put("isrun",isrun);

        boolean isover = false;
        if(status==5)
            isover = true;
        result.put("isover",isover);

        boolean isqueue = false;
        if(status==1){
            queue=queue-1;
            isqueue = true;
        }
        result.put("isqueue",isqueue);
        result.put("numwait",queue);

        boolean isloading = false;
        if(status==2||status==3){
            isloading = true;
        }
        result.put("isloading",isloading);

        return result;
    }

    public static long remaintime(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime givenDateTime = LocalDateTime.parse(time, formatter);
        LocalDateTime currentDateTime = LocalDateTime.now();
        long minutesDifference = ChronoUnit.MINUTES.between(currentDateTime, givenDateTime);
        return minutesDifference;
    }

    public static List<Subjects> getsub(String input){
        String[] substrings = input.split(",");
        List<String> substringList = Arrays.asList(substrings);
        List<Subjects> subjectsList = new ArrayList<>();
        for (String s:substringList){
            subjectsList.add(new Subjects(s));
        }
        return subjectsList;
    }



}
